package com.myhexin.demo.ignite.test.base;

import org.apache.ignite.Ignition;
import org.apache.ignite.client.ClientCache;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author zhangzhonglv@myhexin.com
 * @date 2025/8/6
 */
public class IgniteDemo {

    private static final String IGNITE_ADDR = "127.0.0.1:10800";

    public static void main(String[] args) {
        ClientConfiguration cfg = new ClientConfiguration().setAddresses(IGNITE_ADDR);
        cfg.setPartitionAwarenessEnabled(true);
        try (IgniteClient client = Ignition.startClient(cfg)) {
            ClientCache<Integer, String> cache = client.getOrCreateCache("myCache");
            System.out.println(client.cacheNames());
            Map<Integer, String> data = IntStream.rangeClosed(1, 100).boxed()
                    .collect(Collectors.toMap(i -> i, Object::toString));
            cache.putAll(data);

            assert !cache.replace(1, "2", "3");
            assert "1".equals(cache.get(1));
            assert cache.replace(1, "1", "3");
            assert "3".equals(cache.get(1));

            cache.put(101, "101");
            cache.removeAll(data.keySet());
            assert cache.size() == 1;
            assert "101".equals(cache.get(101));

            cache.removeAll();
            assert 0 == cache.size();
            // Get data from the cache
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
